package web3.Jdbcutils;

import JdbcTemplate.JdbcUtils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Jdbcutils {
    static DataSource dataSource = null;

    static {
        try {
            Properties pro = new Properties();
            InputStream resourceAsStream = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
    //获取连接池

    public static DataSource getDataSource() {
        return dataSource;
    }

    //获取连接从连接池中获取
    public static Connection getconnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        return connection;
    }

    //关闭资源
    public void clazy(ResultSet set, Connection connection, Statement statement) {
        if (set != null) {
            try {
                set.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
